package com.ly.heart.service.impl;

import java.util.List;
import java.util.Optional;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ly.common.PageParam;
import com.ly.common.core.domain.AjaxResult;
import com.ly.common.exception.CustomException;
import com.ly.common.utils.DateUtils;
import com.ly.common.utils.SecurityUtils;
import com.ly.common.utils.StringUtils;
import com.ly.common.utils.erp.ConstUtil;
import com.ly.common.utils.erp.SnowflakeIdWorker;
import com.ly.heart.domain.ActTaskCommentEntity;
import com.ly.heart.domain.dto.StartProcessDto;
import com.ly.common.core.domain.ActCurrentTaskVo;
import com.ly.heart.mapper.ContractProjectMapper;
import com.ly.heart.service.IActModelService;
import com.ly.heart.service.IActTaskCommentService;
import com.ly.heart.service.IContractProjectService;
import org.springframework.stereotype.Service;
import com.ly.heart.domain.ContractProjectEntity;

import javax.annotation.Resource;

/**
 * 合同信息;1:EPC;2:EMC;3:分包;Service业务层处理
 * @author wutao
 * @date 2024-05-31
 */
@Service
public class ContractProjectServiceImpl extends ServiceImpl<ContractProjectMapper, ContractProjectEntity> implements IContractProjectService {

    @Resource
    private ContractProjectMapper contractProjectEntityMapper;
    @Resource
    private IActTaskCommentService iActTaskCommentService;
    @Resource
    private IActModelService iActModelService;

    /**
     * 查询合同信息;1:EPC;2:EMC;3:分包;
     * @param id 合同信息;1:EPC;2:EMC;3:分包;ID
     * @return 合同信息;1:EPC;2:EMC;3:分包;
     */
    @Override
    public ContractProjectEntity selectContractProjectEntityById (String id) {
        return contractProjectEntityMapper.selectContractProjectEntityById ( id );
    }

    /**
     * 查询合同信息;1:EPC;2:EMC;3:分包;列表
     * @param contractProjectEntity 合同信息;1:EPC;2:EMC;3:分包;
     * @return 合同信息;1:EPC;2:EMC;3:分包;
     */
    @Override
    public AjaxResult selectContractProjectEntityList (ContractProjectEntity contractProjectEntity) {
        contractProjectEntity.setPage ( ( contractProjectEntity.getPage () - 1 ) * contractProjectEntity.getLimit () );
        List<ContractProjectEntity> contractProjectEntities = contractProjectEntityMapper.selectContractProjectEntityList ( contractProjectEntity );
        contractProjectEntities.forEach ( project -> {
            ActTaskCommentEntity actTaskCommentEntity = iActTaskCommentService.queryLastComment ( project.getInstanceId () );
            if ( actTaskCommentEntity != null ) {
                project.setApprovedUser ( actTaskCommentEntity.getApprovedUser () );
                project.setApprovedRemark ( actTaskCommentEntity.getRemark () );
            }
            //任务信息
            ActCurrentTaskVo currentTaskVo = iActModelService.getCurrentTaskByInstanceId ( project.getInstanceId () );
            Optional.ofNullable ( currentTaskVo ).ifPresent ( currentTask -> {
                project.setCurrentTask ( currentTask );
            } );
        } );
        Integer count = contractProjectEntityMapper.queryCount ( contractProjectEntity );
        PageParam p = new PageParam ();
        p.setRecords ( contractProjectEntities ).setTotal ( count );
        return AjaxResult.success ( p );
    }

    @Override
    public AjaxResult draft (ContractProjectEntity contractProjectEntity) {
        contractProjectEntity.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
        int count = this.count ();
        contractProjectEntity.setContractNumber ( "GCHT".concat ( String.format ( "%08d", count ) ) );
        contractProjectEntity.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
        contractProjectEntity.setCreateTime ( DateUtils.getNowDate () );
        contractProjectEntity.setState ( ConstUtil.STATE_IN_DRAFT );
        boolean save = this.save ( contractProjectEntity );
        if ( ! save ) {
            throw new CustomException ( "新增工程合同失败！" );
        }
        return AjaxResult.success ( "新增工程合同成功", contractProjectEntity.getId () );
    }

    /**
     * 新增合同信息;1:EPC;2:EMC;3:分包;
     * @param contractProjectEntity 合同信息;1:EPC;2:EMC;3:分包;
     * @return 结果
     */
    @Override
    public AjaxResult submit (ContractProjectEntity contractProjectEntity) {
        if ( StringUtils.isBlank ( contractProjectEntity.getId () ) ) {
            return AjaxResult.error ( "请先暂存该合同！" );
        }
        //创建审批流
        StartProcessDto startDto = new StartProcessDto ().
                setBusinessId ( contractProjectEntity.getId () ).
                setBusinessType ( contractProjectEntity.getBusinessType () ).setModelId ( contractProjectEntity.getModelId () ).
                setBusinessName ( "工程合同_".concat ( contractProjectEntity.getId () ) );
        String instanceId = iActModelService.startProcess ( startDto );
        contractProjectEntity.setInstanceId ( instanceId );
        boolean update = this.update ( new LambdaUpdateWrapper<ContractProjectEntity> ().
                set ( ContractProjectEntity :: getInstanceId, instanceId ).
                set ( ContractProjectEntity :: getState, ConstUtil.STATE_IN_PROCESS ).
                eq ( ContractProjectEntity :: getId, contractProjectEntity.getId () ) );
        if ( ! update ) {
            throw new CustomException ( "提交合同失败！" );
        }
        return AjaxResult.success ( "提交合同成功" );
    }

    /**
     * 修改合同信息;1:EPC;2:EMC;3:分包;
     * @param contractProjectEntity 合同信息;1:EPC;2:EMC;3:分包;
     * @return 结果
     */
    @Override
    public AjaxResult updateContractProjectEntity (ContractProjectEntity contractProjectEntity) {
        contractProjectEntity.setUpdateBy ( SecurityUtils.getLoginUser ().getUser ().getUserId () );
        contractProjectEntity.setUpdateTime ( DateUtils.getNowDate () );
        boolean b = this.updateById ( contractProjectEntity );
        if ( ! b ) {
            throw new CustomException ( "修改工程合同失败！" );
        }
        return AjaxResult.success ();
    }

    /**
     * 批量删除合同信息;1:EPC;2:EMC;3:分包;
     * @param ids 需要删除的合同信息;1:EPC;2:EMC;3:分包;ID
     * @return 结果
     */
    @Override
    public int deleteContractProjectEntityByIds (String[] ids) {
        return contractProjectEntityMapper.deleteContractProjectEntityByIds ( ids );
    }

    /**
     * 删除合同信息;1:EPC;2:EMC;3:分包;信息
     * @param id 合同信息;1:EPC;2:EMC;3:分包;ID
     * @return 结果
     */
    @Override
    public int deleteContractProjectEntityById (String id) {
        return contractProjectEntityMapper.deleteContractProjectEntityById ( id );
    }
}
